791C - Bear and Different Names - CodeForces Solution


constructive algorithms greedy *1500

Please click on ads to support us..

Python Code:

n, k = map(int, input().split())
noticed = list(map(lambda x: x == "YES", input().split()))
names = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'Ba', 'Bb', 'Bc', 'Bd', 'Be', 'Bf', 'Bg', 'Bh', 'Bi', 'Bj',
         'Ca', 'Cb', 'Cc', 'Cd', 'Ce', 'Cf', 'Cg', 'Ch', 'Ci', 'Cj', 'Da', 'Db', 'Dc', 'Dd', 'De', 'Df', 'Dg', 'Dh',
         'Di', 'Dj', 'Ea', 'Eb', 'Ec', 'Ed', 'Ee', 'Ef', 'Eg', 'Eh', 'Ei', 'Ej', 'Fa', 'Fb', 'Fc', 'Fd', 'Fe', 'Ff',
         'Fg', 'Fh', 'Fi', 'Fj', 'Ga', 'Gb', 'Gc', 'Gd', 'Ge', 'Gf', 'Gg', 'Gh', 'Gi', 'Gj', 'Ha', 'Hb', 'Hc', 'Hd',
         'He', 'Hf', 'Hg', 'Hh', 'Hi', 'Hj', 'Ia', 'Ib', 'Ic', 'Id', 'Ie', 'If', 'Ig', 'Ih', 'Ii', 'Ij', 'Ja', 'Jb',
         'Jc', 'Jd', 'Je', 'Jf', 'Jg', 'Jh', 'Ji', 'Jj']
solider = [names[i] for i in range(n)]

t = n
for i in range(len(noticed) - 1, -1, -1):
    if not noticed[i]:
        solider[i] = solider[i + k - 1]
    else:
        t += 1
        solider[i] = names[t]

print(*solider)

C++ Code:

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int,int>
int read() {
	int x = 0,f = 0;char ch = getchar();
	while(!isdigit(ch)) {if(ch == '-')f = 1;ch = getchar();}
	while(isdigit(ch)) {x = x * 10 + ch - '0';ch = getchar();}
	return f ? -x : x;
}
const int N = 1e6 + 10;
int fa[N],vis[N];
int main() {
	int n = read(),m = read();
	for(int i = 1;i <= n - m + 1;i++) {
		string a;
		cin >> a;
		if(a == "NO") vis[i] = 0;
		else vis[i] = 1;
	}
	for(int i = 1;i <= n;i++) fa[i] = i;
	for(int i = n - m + 1;i >= 1;i--) {
		if(vis[i] == 0) {
			fa[i] = fa[i + m - 1];
		}
	}
	for(int i = 1;i <= n;i++) {
		string x = "A";
		int p = fa[i];
		while(p) {
			x += 'a' + p % 26;
			p /= 26;
		}
		cout << x << " ";
	}
}
	  		 	 	   					 					 		  	 	


Comments

Submit
0 Comments
More Questions

1178D - Prime Graph
1711D - Rain
534A - Exam
1472A - Cards for Friends
315A - Sereja and Bottles
1697C - awoo's Favorite Problem
165A - Supercentral Point
1493A - Anti-knapsack
1493B - Planet Lapituletti
747B - Mammoth's Genome Decoding
1591C - Minimize Distance
1182B - Plus from Picture
1674B - Dictionary
1426C - Increase and Copy
520C - DNA Alignment
767A - Snacktower
1365A - Matrix Game
714B - Filya and Homework
31A - Worms Evolution
1691A - Beat The Odds
433B - Kuriyama Mirai's Stones
892A - Greed
32A - Reconnaissance
1236D - Alice and the Doll
1207B - Square Filling
1676D - X-Sum
1679A - AvtoBus
1549A - Gregor and Cryptography
918C - The Monster
4B - Before an Exam